﻿<?php
include_once("../function.php");
//print_r($_COOKIE);die;
//print_r($_GET);die;//Array ( [uname] => zzzzzz [addr] => 上海 [phone] => 15861880507 [remember] => 1 [sub] => 保存 )
extract($_GET);//$uname $addr $phone $remember
if(isset($color))
{
	//$color=substr($color,1);		
}
$usname=trim($usname);
$addr=trim($addr);
$phone=trim($phone);
if(isset($remember))
{
$remember=trim($remember);
}
else
{
	$remember='';
}

//print_r($_COOKIE);die;//Array ( [PHPSESSID] => 6q6po5kumtr2mias1gb0u8m5g1 [usname] => zzzzzz [pwd] => 123456 [rem] => on [username] => zzzzzz )
$name=$_COOKIE['username'];
$sql="select * from ts_user where username='$name'";
$res=mysqli_query($link,$sql);
if(is_object($res))
{
	if(mysqli_num_rows($res)==1)
	{
		$one=mysqli_fetch_assoc($res);
		$a=$one['uid'];
		//print_r($one);die;//4
	}
}

if(isset($remember)&&$remember==1)
{
	$defalut=1;
}
else
{
	$defalut=0;
}
$sql="select * from ts_address where u_id=$a and addr_defalut=1";
//echo $sql;//var_dump(mysqli_query($link,$sql));die;
$res=mysqli_query($link,$sql);
if(is_object($res))
{
	if(mysqli_num_rows($res)>0)
	{
		$list=mysqli_fetch_assoc($res);
		if($defalut==1)
		{
			$sql="update ts_address set addr_defalut=0 where addr_id=".$list['addr_id'];
			//echo $sql;var_dump(mysqli_query($link,$sql));die;
			mysqli_query($link,$sql);
			$sql="insert into ts_address(consignee,address,mobile,addr_defalut,u_id) values('$usname','$addr','$phone','$defalut','$a')";
			$res=mysqli_query($link,$sql);
			if($res)
			{
				if(mysqli_affected_rows($link)==1)
				{
					if(isset($color))
					{
						msg("保存成功","./order.php?type=$type&size=$size&num=$num&price=$price&color=$color&pid=$pid");exit;
					}
					else
					{
						msg("保存成功","./order.php?type=$type");exit;
					}
				}
				else
				{
					msg("保存有误","./order.php?type=$type");exit;
				}
				
			}
		}
		elseif($defalut==0)
		{
			$sql="insert into ts_address(consignee,address,mobile,addr_defalut,u_id) values('$usname','$addr','$phone','$defalut','$a')";
			$res=mysqli_query($link,$sql);
			if($res)
			{
				if(mysqli_affected_rows($link)==1)
				{
					if(isset($color))
					{
						msg("保存成功","./order.php?type=$type&size=$size&num=$num&price=$price&color=$color&pid=$pid");exit;
					}
					else
					{
						msg("保存成功","./order.php?type=$type");exit;
					}
				}
				else
				{
					msg("保存有误","./order.php?type=$type");exit;
				}
				
			}
		}
	}

	else
	{
		$sql="insert into ts_address(consignee,address,mobile,addr_defalut,u_id) values('$usname','$addr','$phone','1','$a')";
			$res=mysqli_query($link,$sql);
			if($res)
			{
				if(mysqli_affected_rows($link)==1)
				{
					if(isset($color))
					{
						msg("保存成功","./order.php?type=$type&size=$size&num=$num&price=$price&color=$color&pid=$pid");exit;
					}
					else
					{
						msg("保存成功","./order.php?type=$type");exit;
					}
				}
				else
				{
					msg("保存有误","./order.php?type=$type");exit;
				}
				
			}
	}

}

?>